package dk.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import dk.Service.TokenService;
import dk.beans.user;
import dk.mapper.usermapper;

@RestController
public class UserController {
@Autowired
usermapper usermapper;
@Autowired
TokenService TokenService;
@PostMapping("ChangePasswordByToken")
public ResponseData ChangePasswordByToken(String oldpassword,String newpassword,HttpServletRequest request){
	System.out.println("ChangePasswordByToken");
	ResponseData data = new ResponseData();
	String token = request.getHeader("token");
	Integer id = TokenService.GetIdByToken(token);
	System.out.println(id);
	if(id != 0) {
		//根据id更改密码
		QueryWrapper<user> qw = new QueryWrapper<user>();
		qw.eq("id", id);
		user user =usermapper.selectOne(qw);
		String userpassword = user.getUserpassword();
		System.out.println(userpassword);
		System.out.println(oldpassword);
		System.out.println(newpassword);
		if(userpassword.equals(oldpassword)) {
			user.setUserpassword(newpassword);
			usermapper.update(user, qw);
			data.setCode(200);
			data.setMessage("密码修改成功,新密码将在下次登录启用");
		}
		else {
			data.setData(201);
			data.setMessage("请输入正确密码");
		}
		
	}
	else {
		data.setCode(201);
		data.setMessage("非法请求");
	}
	return data;
}
{
	
}

}
